<template>
  <el-dialog title="创建分享展示" :close-on-click-modal="false" :destroy-on-close="true" width="800px" :visible.sync="isShow" :before-close="closeBox">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item prop="title" label="标题名称">
        <el-input v-model="ruleForm.title" autocomplete="off" placeholder="请输入课题名称"></el-input>
      </el-form-item>
      <el-form-item prop="isAppraised" label="评价开关">
        <el-radio-group v-model="ruleForm.isAppraised">
          <el-radio :label="1">开启</el-radio>
          <el-radio :label="0">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
      <div class="online-dialogTitle">选择学生</div>
      <div class="online-studentBox" v-for="(item, index) in list" :key="index">
        <div class="online-dialogTitle" style="padding-left: 15px;width: 100%;">{{item.className}}</div>
        <div class="online-stu" v-for="(it, ind) in item.studentList" :key="ind" @click="joinList(it)">
          <i class="el-icon-success" v-if="ruleForm.studentIds.indexOf(it.studentId) > -1"></i>
          <img class="online-studentLogo" :src="it.headImgUrl"/>
          <div class="online-studentName">{{it.name}}</div>
        </div>
      </div>
      <div class="online-dialogTitle">选择资源</div>
      <el-table ref="multipleTable" :data="resourceList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column label="文件类型">
          <template slot-scope="scope">
            <span v-if="scope.row.fileType === '1'">文本</span>
            <span v-if="scope.row.fileType === '2'">图像</span>
            <span v-if="scope.row.fileType === '3'">音频</span>
            <span v-if="scope.row.fileType === '4'">视频</span>
          </template>
        </el-table-column>
        <el-table-column label="文件名称">
          <template slot-scope="scope">{{ scope.row.fileName }}</template>
        </el-table-column>
      </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: center;">
      <el-button type="primary" class="my-button" @click="submitForm('ruleForm')">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getOnlineStuList, getOnlineResource, saveLessonShare } from '@/api/teacher/onlineCourse'
export default {
  props: ['id'],
  data () {
    return {
      isShow: true,
      list: [],
      ruleForm: {
        onlineLessonId: this.id,
        title: undefined,
        isAppraised: 1,
        resourceIds: [],
        studentIds: [],
        studentNames: []
      },
      rules: {
        title: [{ required: true, message: '请输入标题名称', trigger: 'blur' }]
      },
      resourceList: []
    }
  },
  methods: {
    async getOnlineStuList () {
      const res = await getOnlineStuList({ onlineLessonId: this.id })
      if (res.code === '200') {
        this.list = res.data
      }
    },
    async getOnlineResource () {
      const res = await getOnlineResource({ onlineLessonId: this.id })
      if (res.code === '200') {
        this.resourceList = res.data
      }
    },
    async saveLessonShare (data) {
      const res = await saveLessonShare(data)
      if (res.code === '200') {
        this.$message.success('操作成功')
        // 重新刷新活动列表
        this.$emit('handleEmit', 'confirm')
      }
    },
    joinList (item) {
      const index = this.ruleForm.studentIds.indexOf(item.studentId)
      const index2 = this.ruleForm.studentNames.indexOf(item.name)
      if (index > -1) {
        this.ruleForm.studentIds.splice(index, 1)
        this.ruleForm.studentNames.splice(index2, 1)
      } else {
        this.ruleForm.studentIds.push(item.studentId)
        this.ruleForm.studentNames.push(item.name)
      }
    },
    handleSelectionChange (val) {
      this.ruleForm.resourceIds = val.map(item => item.id)
    },
    submitForm (ruleForm) {
      this.$refs[ruleForm].validate(async valid => {
        if (valid) {
          if (this.ruleForm.studentIds.length < 1 || this.ruleForm.studentNames.length < 1) {
            this.$message.error('请选择要分享的学生')
            return
          }
          if (this.ruleForm.resourceIds.length < 1) {
            this.$message.error('请选择要分享的资源')
            return
          }
          const data = {
            resourceIds: this.ruleForm.resourceIds,
            studentIds: this.ruleForm.studentIds,
            studentNames: this.ruleForm.studentNames,
            title: this.ruleForm.title,
            isAppraised: this.ruleForm.isAppraised,
            onlineLessonId: this.id
          }
          this.saveLessonShare(data)
        } else {
          return false
        }
      })
    },
    closeBox () {
      this.$emit('handleEmit')
    }
  },
  mounted () {
    this.getOnlineStuList()
    this.getOnlineResource()
  }
}
</script>
